using System;
using System.Collections.Generic;

namespace Nice157Advices.Advices
{
    /// <summary>
    /// 10、创建对象时需要考虑是否实现比较器
    /// </summary>
    public static class _10
    {
        /**
         * Sort方法可自动排序对象列表，如果该对象实现了IComparable接口
         */

        public static void Go ()
        {
            var list = new List<Salary> ()
            {
                new Salary { Name = "name1", BaseSaraly = 1000, Bouns = 500 },
                new Salary { Name = "name2", BaseSaraly = 500, Bouns = 700 },
                new Salary { Name = "name3", BaseSaraly = 5000, Bouns = 200 },
                new Salary { Name = "name4", BaseSaraly = 700, Bouns = 400 }
            };

            list.Sort ();

            Console.WriteLine ("use implementing IComparable");
            list.ForEach (u => Console.WriteLine ($"{u.Name} BaseSaraly: {u.BaseSaraly} Bouns:{u.Bouns}"));

            list.Sort (new BounsComparer ());

            Console.WriteLine ("use custom comparer");
            list.ForEach (u => Console.WriteLine ($"{u.Name} BaseSaraly: {u.BaseSaraly} Bouns:{u.Bouns}"));
        }

        private class BounsComparer : IComparer<Salary>
        {
            public int Compare (Salary x, Salary y) => x.Bouns.CompareTo (y.Bouns);
        }

        private class Salary : IComparable<Salary>
        {
            public string Name { get; set; }
            public int BaseSaraly { get; set; }
            public int Bouns { get; set; }
            public int CompareTo (Salary other)
            {
                return BaseSaraly.CompareTo (other.BaseSaraly);
            }
        }
    }
}